// 1.引入 vue 和 vue-router
import Vue from 'vue'
import VueRouter from 'vue-router'

// 将VueRouter 安装为 vue的插件
Vue.use(VueRouter)

// 创建路由实例
const router = new VueRouter({
  routes:[
    {path:'/login',component: () => import('@/components/Login.vue')},
    {
      path:'/',
      redirect:'/user',
      component: () => import('@/components/Home.vue'),
      children:[
        {path:'/user',component:() => import('@/components/menus/User.vue')},
        {path:'/right',component:() => import('@/components/menus/Right.vue')},
        {path:'/good',component:() => import('@/components/menus/Good.vue')},
        {path:'/order',component:() => import('@/components/menus/Order.vue')},
        {path:'/setting',component:() => import('@/components/menus/Setting.vue')},
        {path:'/userDetail/:id',component:() => import('@/components/user/UserDetail.vue')}
      ]
    }
  ]
})

router.beforeEach((to,from,next) => {
  if(to.path === '/login'){
    next()
  } else{
    const token = localStorage.getItem('token')
    if(token){
      // 已经登录
      next()
    }else {
      // 没有登录
      next('/login')
    }
  }
})
// 导出路由实例对象
export default router